var areaList = [];
var storefrontList = [];
$("#dop_datagrid_deliveryman_relate").datagrid({
	url : "",
	height : 350,
	pagination : true,
	fitColumns : false,
	singleSelect : true,
	fit : false,
	pageSize : 10,
	pageList : [10, 30, 50, 100],
	sortOrder : 'desc',
	rownumbers : true,
	showFooter : true,
	frozenColumns : [[]],
	columns : [[{
		field : "id",
		width : 150,
		title : "主键",
		hidden : true
	}, {
		field : "delName",
		width : 150,
		title : "员工姓名",
		align:'center'
	},{
		field : "delTelephone",
		width : 150,
		title : "手机",
		halign:"center",
		align:'left'
	}, {
		field : "areaId",
		width : 150,
		title : "已关联区域ID",
		hidden : true
	}, {
		field : "areaName",
		width : 400,
		title : "已关联区域名称",
		halign:"center",
		align:'left'
	}, {
		field : "storefrontId",
		width : 500,
		title : "已关联店面ID",
		hidden : true
	}, {
		field : "storefrontName",
		width : 400,
		title : "已关联店面名称",
		halign:"center",
		align:'left'
	}]]
});



	

// get params
function getQueryParams() {
	return {
		delName : $("input[code='delName']").val(),
		delCertificates : $("input[code='delCertificates']").val(),
		delTelephone : $("input[code='delTeliPhone']").val()
	};
}


// init
function init() {

	// init
	var options = $("#dop_datagrid_deliveryman_relate").datagrid('getPager').data("pagination").options;
	var pageSize = options.pageSize;
	var params = $.extend({
		pageNum : 1,
		pageSize : pageSize
	}, getQueryParams());
	EapTools.doAction("deliveryManController.do?queryList", params, false,
			function(data) {
				$("#dop_datagrid_deliveryman_relate").datagrid('loadData', {
					"rows" : data.list,
					"total" : data.count
				} || {});
				var p = $("#dop_datagrid_deliveryman_relate").datagrid('getPager');
				$("td:last", p).find("a").click();
			});
	// click pagination
	var p = $("#dop_datagrid_deliveryman_relate").datagrid('getPager');
	$(p).pagination(
			{
				onSelectPage : function(pageNumber, pageSize) {
					var params = $.extend({
						pageNum : pageNumber,
						pageSize : pageSize
					}, getQueryParams());
					EapTools.doAction("deliveryManController.do?queryList", params,
							false, function(data) {
								$("#dop_datagrid_deliveryman_relate").datagrid('loadData',
										{
											"rows" : data.list,
											"total" : data.count
										} || {});
							});
				}
			});
}

$(function(){
	init();
});



// bind assign event
$("#dop_btn_deliveryman_assign")
		.click(
				function() {
					var selections = $("#dop_datagrid_deliveryman_relate")
							.datagrid("getSelections");
					if (selections && selections.length > 0) {
						if (selections.length > 1) {
							layer.tips('请选择一行数据', this, {
								guide : 2,
								time : 2
							});
						} else {
							var selectedAreaId = $("#area").val();
							var storefrontId = $("#storefront").val();
							EapTools
									.doAction(
											"deliveryManRelateController.do?assignAreaAndStorefront",
											{
												id : $(
														"#dop_datagrid_deliveryman_relate")
														.datagrid(
																"getSelections")[0].id,
												areaIdList : selectedAreaId,
												storefrontIdList : storefrontId
											}, false, function(data) {
												if (data.success) {
													EapTip.msgOk("分配成功");
												} else {
													EapTip.msgError("分配失败");
												}
												init();
											});
						}
					} else
						layer.tips('请选择要修改行', this, {
							guide : 2,
							time : 2
						});
				});

				
/**
 * 绑定keydown事件,只允许输入数字
 */
$("input[code='delMaxmum'],input[code='delTeliPhone'],#delMaxmum").keydown(function(e){
	e = e || event;	
	var code=e.keyCode;
	return (code>36&&code<41)||(code>45&&code<58)||(code>95&&code<107)||code==8||code==9;
});

// bind query event
$("#dop_btn_deliveryman_query").click(function() {
	init();
});


$(function(){
	
	// 查询区域信息
	EapTools.doAction("areaController.do?findData", {},
			true, function(data) {
				var areaSelect=$("#area");
				// 区域
				for (var i = 0; i < data.length; i++) {
					areaSelect.append(
							"<option value='"
									+ data[i].id
									+ "'>"
									+ data[i].baseAreaName
									+ "</option>");
				}
				areaSelect.multipleSelect({
					filter : true
				});
			});
	// 查询店面信息
	EapTools.doAction("storefrontController.do?findData", {}, true, function(
			data) {
			var storeSelect=$("#storefront");
			for (var i = 0; i < data.length; i++) {
				storeSelect.append(
						"<option value='"
								+ data[i].id
								+ "'>"
								+ data[i].stoName
								+ "</option>");
			}
			storeSelect.multipleSelect({
				filter : true
			});
	});
	
});

//行切换
$("#dop_datagrid_deliveryman_relate")
		.datagrid(
				{
					onClickRow : function() {
						var selections = $("#dop_datagrid_deliveryman_relate")
								.datagrid("getSelections");
						var deliveryManId = selections[0].id;

						// 查询区域信息
						EapTools.doAction("areaController.do?findData", {},
								false, function(data) {
									areaList = data;
								});
						// 查询店面信息
						EapTools.doAction("storefrontController.do?findData", {}, false, function(
								data) {
							storefrontList=data;
						});

						$("#area").empty();
						$("#storefront").empty();

						// 查询员工姓名信息
						EapTools
								.doAction(
										"deliveryManController.do?getById",
										{
											id : deliveryManId
										},
										false,
										function(data) {
											var selectedAreaIdList = data.areaIdList;
											// 区域
											for (var i = 0; i < areaList.length; i++) {

												//如果选中的区域ID为空，则说明之前用户没有选中任何数据，则显示所有
												if (selectedAreaIdList == "undefined"
														|| selectedAreaIdList.length == 0) {
													$("#area")
															.append(
																	"<option value='"
																			+ areaList[i].id
																			+ "'>"
																			+ areaList[i].baseAreaName
																			+ "</option>");
												}

												for (var j = 0; j < selectedAreaIdList.length; j++) {
													if (selectedAreaIdList[j] === areaList[i].id) {
														$("#area")
																.append(
																		"<option value='"
																				+ areaList[i].id
																				+ "'  selected='selected'>"
																				+ areaList[i].baseAreaName
																				+ "</option>");
														break;
													}
													// 最后一个还是不等，则说明没有勾选
													if (j === selectedAreaIdList.length - 1) {
														$("#area")
																.append(
																		"<option value='"
																				+ areaList[i].id
																				+ "'>"
																				+ areaList[i].baseAreaName
																				+ "</option>");
													}
												}
											}
											// 店面
											var selectedStorefrontIdList = data.storefrontIdList;
											for (var i = 0; i < storefrontList.length; i++) {
												if (selectedStorefrontIdList == "undefined"
														|| selectedStorefrontIdList.length == 0) {
													$("#storefront")
															.append(
																	"<option value='"
																			+ storefrontList[i].id
																			+ "'>"
																			+ storefrontList[i].stoName
																			+ "</option>");
												}

												for (var j = 0; j < selectedStorefrontIdList.length; j++) {
													if (selectedStorefrontIdList[j] === storefrontList[i].id) {
														$("#storefront")
																.append(
																		"<option value='"
																				+ storefrontList[i].id
																				+ "'  selected='selected'>"
																				+ storefrontList[i].stoName
																				+ "</option>");
														break;
													}
													// 最后一个还是不等，则说明没有勾选
													if (j === selectedStorefrontIdList.length - 1) {
														$("#storefront")
																.append(
																		"<option value='"
																				+ storefrontList[i].id
																				+ "'>"
																				+ storefrontList[i].stoName
																				+ "</option>");
													}
												}
											}
										});
						$("#area").multipleSelect({
							filter : true
						});
						$("#storefront").multipleSelect({
							filter : true
						});
					}
				});

